<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\User;

use App\Constants\Constants;
use App\Dao\AgentDao;
use App\Dao\AgentMoneyDao;
use App\Dao\BusinessDao;
use App\Dao\IntegralDetailDao;
use App\Dao\MoneyDao;
use App\Dao\UserDao;
use App\Exception\CustomException;
use App\Job\IntegralJob;
use App\Kernel\Util\UserJwtInstance;
use App\Model\Goods;
use App\Model\GoodsSku;
use App\Model\IntegralDetail;
use App\Model\OrderItem;
use App\Service\Service;

use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

/**
 * 金额服务
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\Admin
 */
class MoneyService extends Service
{

    /**
     * @Inject
     * @var ScanOrderService
     */
    public $scan_order_service;

    /**
     * @Inject
     * @var MoneyDao
     */
    public $money_dao;

    /**
     * @Inject
     * @var AgentMoneyDao
     */
    public $agent_money_dao;

    /**
     * @Inject
     * @var AgentDao
     */
    public $agent_dao;

    /**
     * @Inject
     * @var BusinessDao
     */
    public $business_dao;

    /**
     * @Inject
     * @var UserDao
     */
    public $user_dao;

    /**
     * @Inject
     * @var OrderCouponService
     */
    public $order_coupon_service;

    /**
     * @Inject
     * @var IntegralDetailDao
     */
    public $integral_detail;

    /**
     * @Inject
     * @var \App\Service\Agent\MoneyService
     */
    private $agent_monet_service;

    /**
     * 商家流水
     * @param string $start_time
     * @return mixed
     */
    public function list(string $start_time)
    {
        $user = UserJwtInstance::instance()->build()->getUser();
        $business = $user->business;
        if ( empty($business) ) $this->error();
        // 本月第一天
        $first = date('Y-m-01 0:0:0', strtotime($start_time));
        // 本月最后一天
        $last = date('Y-m-d 23:59:59', strtotime("$first +1 month -1 day"));

        $whereMap['business_id'] = $business->id;
        $whereMap['status'] = $this->money_dao::STATUS_ENABLE;
        $whereMap['type'] = $this->money_dao::INCOME_TYPE;

        $result = $this->money_dao->money($whereMap, strtotime($first), strtotime($last));

        $month_total = $this->money_dao->monthMoney($whereMap,strtotime($first), strtotime($last));

        $data = [];
        foreach ($result as $key => $value) {

            $data['list'][$key]['created_at'] = date('Y-m-d H:i', $value['created_at']);
            $data['list'][$key]['actual_money'] = (float)$value['num'];
            $data['list'][$key]['platform_money'] = $value['service_charge'];
            $data['list'][$key]['amount'] = $value['amount'];
            $data['list'][$key]['order_no'] = $value['order_no'];
            $data['list'][$key]['name'] = $value['lower']['name'];
            $data['list'][$key]['avatar'] = $value['lower']['avatar'];
        }
        $data['month_total'] = $month_total;

        return [
            'total_money' => $this->getMoneyBalance($business->id),
            'list' => $data
        ];
    }


    /**
     * 线下扫码商家分佣
     * @param string $order_no
     * @return mixed
     */
    public function ScanBusinessBonus(string $order_no)
    {
        $scan_order = $this->scan_order_service->scan_order_dao->first([
            'order_no' => $order_no
        ]);

        if( $scan_order['status'] != '1' ) {
            $this->error();
        }

        $business = $this->business_dao->first([
            'id' => $scan_order['business_id']
        ],['id','user_id', 'name', 'logo','parent_id','district_id','city_id','tel']);

        $bonus = filterMoney($scan_order['amount'] * 0.85,4);
        Db::beginTransaction();
        try {

            //上级会员
            $user = $this->user_dao->first($scan_order['user_id']);

            if( $user['parent_id'] ) {
                $parentFirst = $this->user_dao->first($user['parent_id'],false);

                if( $parentFirst['id'] && $parentFirst['group_id'] == 1 ) {
                    // 写入积分记录
                    $this->integral_detail->create([
                        'user_id' => $parentFirst['id'],
                        'lower_id' => $user['id'],
                        'status' => $this->integral_detail::SHARE_INTEGRAL,
                        'type' => $this->integral_detail::INCOME_TYPE,
                        'num' => filterMoney($scan_order['amount'] * 0.05,4),
                        'remark' => '兑换商品',
                        'order_no' => $scan_order['order_no'],
                        'goods_name' => $user['name'],
                        'goods_pic' => $user['avatar'],
                        'amount' => $scan_order['amount'],
                    ]);

                    $this->driver->push(new IntegralJob( $parentFirst['id'] ));
                }

                if( $parentFirst['parent_id'] && $parentFirst['group_id'] == 2 ) {
                    // 写入金额记录
                    $this->money_dao->create([
                        'status' => $this->money_dao::STATUS_ENABLE,
                        'user_id' => $parentFirst['id'],
                        'lower_id' => $scan_order['user_id'],
                        'type' => $this->money_dao::INCOME_TYPE,
                        'order_no' => $scan_order['order_no'],
                        'source' => $this->money_dao::SOURCE_SCAN,
                        'price_source' => 1,
                        'num' => filterMoney($scan_order['amount'] * 0.005,4),
                        'created_at' => time(),
                        'goods_name' => $user['name'],
                        'goods_pic' => $user['avatar'],
                        'amount' => $scan_order['amount'] ,
                    ]);
                    $this->updateMoneyUserBalance($parentFirst['id']);
                }
            }

            //商家上级渠道商
            $channel_user = $this->user_dao->first($business['parent_id']);

            if( $channel_user ) {
                $this->money_dao->create([
                    'status' => $this->money_dao::STATUS_ENABLE,
                    'user_id' => $channel_user['id'],
                    'lower_id' => $business['user_id'],
                    'type' => $this->money_dao::INCOME_TYPE,
                    'order_no' => $scan_order['order_no'],
                    'source' => $this->money_dao::SOURCE_SCAN,
                    'price_source' => 2,
                    'num' => filterMoney($scan_order['amount'] * 0.015,4),
                    'created_at' => time(),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'amount' => $scan_order['amount'] ,
                ]);
                $this->updateMoneyUserBalance($channel_user['id']);
            }

            //区代
            $areaAgent = $this->agent_dao->map([
                'location_id' => $business['district_id']
            ],false);

            if( !empty($areaAgent) && $areaAgent['identity'] == 1 ) {
                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $areaAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'type' => 1,
                    'order_no' => $scan_order['order_no'],
                    'source' => 1,
                    'price_source' => 1,
                    'num' => filterMoney($scan_order['amount'] * $areaAgent['area_agent_money'],4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $scan_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
                $this->agent_monet_service->updateMoneyAgentBalance($areaAgent['id']);
            }

            //市代
            $cityAgent = $this->agent_dao->map([
                'location_id' => $business['city_id']
            ],false);

            if( !empty($cityAgent) && $cityAgent['identity'] == 2 ) {
                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $cityAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'type' => 1,
                    'order_no' => $scan_order['order_no'],
                    'source' => 2,
                    'price_source' => 1,
                    'num' => filterMoney($scan_order['amount'] * $cityAgent['city_agent_money'],4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $scan_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
                $this->agent_monet_service->updateMoneyAgentBalance($cityAgent['id']);
            }
            //区代分公司
            $partnerAreaAgent = $this->agent_dao->map([
                'id' => $areaAgent['inviter_id']
            ],false);

            if( !empty($partnerAreaAgent) && $partnerAreaAgent['identity'] == 3  ) { //区代分公司
                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $partnerAreaAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'area_id' => $areaAgent['id'],
                    'type' => 1,
                    'order_no' => $scan_order['order_no'],
                    'source' => 1,
                    'price_source' => 1,
                    'num' => filterMoney($scan_order['amount'] * $partnerAreaAgent['area_agent_money'],4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $scan_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
                $this->agent_monet_service->updateMoneyAgentBalance($partnerAreaAgent['id']);
            } else if( !empty($partnerAreaAgent) && $partnerAreaAgent['identity'] == 4 ) {//合伙人
                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $partnerAreaAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'type' => 1,
                    'order_no' => $scan_order['order_no'],
                    'source' => 1,
                    'price_source' => 1,
                    'num' => filterMoney($scan_order['amount'] * $partnerAreaAgent['area_agent_money'],4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $scan_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
                $this->agent_monet_service->updateMoneyAgentBalance($partnerAreaAgent['id']);
                $branchAreaAgent = $this->agent_dao->map([
                    'id' => $partnerAreaAgent['inviter_id']
                ],false); //合伙人查询分公司

                $branchAgentAmount = $branchAreaAgent['area_agent_money'] - $partnerAreaAgent['area_agent_money'];

                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $branchAreaAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'partner_id' => $partnerAreaAgent['id'],
                    'type' => 1,
                    'order_no' => $scan_order['order_no'],
                    'source' => 3,
                    'price_source' => 3,
                    'num' => filterMoney($scan_order['amount'] * $branchAgentAmount,4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $scan_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
                $this->agent_monet_service->updateMoneyAgentBalance($branchAreaAgent['id']);
            }

            //市代分公司
            $partnerCityAgent = $this->agent_dao->map([
                'id' => $cityAgent['inviter_id']
            ],false);

            if( !empty($partnerCityAgent) && $partnerCityAgent['identity'] == 3  ) { //区代分公司
                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $partnerCityAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'city_id' => $cityAgent['id'],
                    'type' => 1,
                    'order_no' => $scan_order['order_no'],
                    'source' => 2,
                    'price_source' => 1,
                    'num' => filterMoney($scan_order['amount'] * $partnerCityAgent['city_agent_money'],4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $scan_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
                $this->agent_monet_service->updateMoneyAgentBalance($partnerCityAgent['id']);
            } else if( !empty($partnerCityAgent) && $partnerCityAgent['identity'] == 4 ) {//合伙人
                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $partnerCityAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'type' => 1,
                    'order_no' => $scan_order['order_no'],
                    'source' => 2,
                    'price_source' => 1,
                    'num' => filterMoney($scan_order['amount'] * $partnerCityAgent['city_agent_money'],4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $scan_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
                $this->agent_monet_service->updateMoneyAgentBalance($partnerCityAgent['id']);
                $branchCityAgent = $this->agent_dao->map([
                    'id' => $partnerCityAgent['inviter_id']
                ],false); //合伙人查询分公司

                $branchAgentAmount = $branchCityAgent['city_agent_money'] - $partnerCityAgent['city_agent_money'];

                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $branchCityAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'partner_id' => $partnerCityAgent['id'],
                    'type' => 1,
                    'order_no' => $scan_order['order_no'],
                    'source' => 3,
                    'price_source' => 4,
                    'num' => filterMoney($scan_order['amount'] * $branchAgentAmount,4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $scan_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
                $this->agent_monet_service->updateMoneyAgentBalance($branchCityAgent['id']);
            }

            //商家分润
            $this->money_dao->create([
                'status' => $this->money_dao::STATUS_ENABLE,
                'user_id' => $business['user_id'],
                'lower_id' => $scan_order['user_id'],
                'business_id' => $business['id'],
                'type' => $this->money_dao::INCOME_TYPE,
                'order_no' => $scan_order['order_no'],
                'source' => $this->money_dao::SOURCE_SCAN,
                'num' => $bonus,
                'created_at' => time(),
                'goods_name' => $business['name'],
                'goods_pic' => $business['logo'],
                'amount' => $scan_order['amount'] ,
                'service_charge' => filterMoney($scan_order['amount']-$bonus,4 ),
            ]);

            $this->updateMoneyBalance($business['id']);

            Db::commit();
        }
        catch (CustomException $e) {
            Db::rollBack();
            $this->error($e->getMessage());
        }

    }

    /**
     * 核销商家分润
     * @param string $order_no
     * @return mixed
     */
    public function writeBusinessBonus(string $order_no)
    {
        $coupon_order = $this->order_coupon_service->dao->first([
            'order_no' => $order_no
        ]);

        $business = $this->business_dao->first([
            'id' => $coupon_order['business_id']
        ],['id','user_id', 'name', 'logo','parent_id']);

        Db::beginTransaction();
        try {
            $bonus = filterMoney($coupon_order['price'] * 0.85,4);

            //上级会员
            $user = $this->user_dao->first($coupon_order['user_id']);

            if($user['parent_id']) {
                $parentFirst = $this->user_dao->first($user['parent_id'],false);

                if( $parentFirst['id'] && $parentFirst['group_id'] == 1 ) {
                    // 写入积分记录
                    $this->integral_detail->create([
                        'user_id' => $parentFirst['id'],
                        'lower_id' => $user['id'],
                        'status' => $this->integral_detail::SHARE_INTEGRAL,
                        'type' => $this->integral_detail::INCOME_TYPE,
                        'num' => filterMoney($coupon_order['price'] * 0.05,4),
                        'remark' => '兑换商品',
                        'order_no' => $coupon_order['order_no'],
                        'goods_name' => $user['name'],
                        'goods_pic' => $user['avatar'],
                        'amount' => $coupon_order['price'] ,
                    ]);

                    $this->driver->push(new IntegralJob( $parentFirst['id'] ));
                }

                if( $parentFirst['id'] && $parentFirst['group_id'] == 2 ) {
                    // 写入积分记录
                    $this->money_dao->create([
                        'status' => $this->money_dao::STATUS_ENABLE,
                        'user_id' => $parentFirst['id'],
                        'lower_id' => $coupon_order['user_id'],
                        'type' => $this->money_dao::INCOME_TYPE,
                        'order_no' => $coupon_order['order_no'],
                        'source' => $this->money_dao::SOURCE_SCAN,
                        'price_source' => 1,
                        'num' => filterMoney($coupon_order['price'] * 0.005,4),
                        'created_at' => time(),
                        'goods_name' => $user['name'],
                        'goods_pic' => $user['avatar'],
                        'amount' => $coupon_order['price'] ,
                    ]);
                    $this->updateMoneyUserBalance($parentFirst['id']);
                }
            }

            //商家上级渠道商
            $channel_user = $this->user_dao->first($business['parent_id']);

            if( $channel_user ) {
                $this->money_dao->create([
                    'status' => $this->money_dao::STATUS_ENABLE,
                    'user_id' => $channel_user['id'],
                    'lower_id' => $business['user_id'],
                    'type' => $this->money_dao::INCOME_TYPE,
                    'order_no' => $coupon_order['order_no'],
                    'source' => $this->money_dao::SOURCE_SCAN,
                    'price_source' => 2,
                    'num' => filterMoney($coupon_order['price'] * 0.015,4),
                    'created_at' => time(),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'amount' => $coupon_order['price'],
                ]);
                $this->updateMoneyUserBalance($channel_user['id']);

            }

            $areaAgent = $this->agent_dao->map([
                'location_id' => $business['district_id']
            ],false);

            if( !empty($areaAgent) && $areaAgent['identity'] == 1 ) {
                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $areaAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'type' => 1,
                    'order_no' => $coupon_order['order_no'],
                    'source' => 1,
                    'price_source' => 1,
                    'num' => filterMoney($coupon_order['amount'] * $areaAgent['area_agent_money'],4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $coupon_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
            }

            $cityAgent = $this->agent_dao->map([
                'location_id' => $business['city_id']
            ],false);

            if( !empty($cityAgent) && $cityAgent['identity'] == 2 ) {
                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $cityAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'type' => 1,
                    'order_no' => $coupon_order['order_no'],
                    'source' => 2,
                    'price_source' => 1,
                    'num' => filterMoney($coupon_order['amount'] * $cityAgent['city_agent_money'],4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $coupon_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
            }

            $partnerAreaAgent = $this->agent_dao->map([
                'id' => $areaAgent['inviter_id']
            ],false);

            if( !empty($partnerAreaAgent) && $partnerAreaAgent['identity'] == 3  ) { //区代分公司
                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $partnerAreaAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'type' => 1,
                    'order_no' => $coupon_order['order_no'],
                    'source' => 1,
                    'price_source' => 1,
                    'num' => filterMoney($coupon_order['amount'] * $partnerAreaAgent['area_agent_money'],4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $coupon_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
            } else if( !empty($partnerAreaAgent) && $partnerAreaAgent['identity'] == 4 ) {//合伙人
                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $partnerAreaAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'type' => 1,
                    'order_no' => $coupon_order['order_no'],
                    'source' => 1,
                    'price_source' => 1,
                    'num' => filterMoney($coupon_order['amount'] * $partnerAreaAgent['area_agent_money'],4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $coupon_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);

                $branchAreaAgent = $this->agent_dao->map([
                    'id' => $partnerAreaAgent['inviter_id']
                ],false); //合伙人查询分公司

                $branchAgentAmount = $branchAreaAgent['area_agent_money'] - $partnerAreaAgent['area_agent_money'];

                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $branchAreaAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'partner_id' => $partnerAreaAgent['id'],
                    'type' => 1,
                    'order_no' => $coupon_order['order_no'],
                    'source' => 3,
                    'price_source' => 3,
                    'num' => filterMoney($coupon_order['amount'] * $branchAgentAmount,4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $coupon_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
            }

            $partnerCityAgent = $this->agent_dao->map([
                'id' => $cityAgent['inviter_id']
            ],false);

            if( !empty($partnerCityAgent) && $partnerCityAgent['identity'] == 3  ) { //区代分公司
                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $partnerCityAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'type' => 1,
                    'order_no' => $coupon_order['order_no'],
                    'source' => 2,
                    'price_source' => 1,
                    'num' => filterMoney($coupon_order['amount'] * $partnerCityAgent['city_agent_money'],4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $coupon_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
            } else if( !empty($partnerCityAgent) && $partnerCityAgent['identity'] == 4 ) {//合伙人
                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $partnerCityAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'type' => 1,
                    'order_no' => $coupon_order['order_no'],
                    'source' => 2,
                    'price_source' => 1,
                    'num' => filterMoney($coupon_order['amount'] * $partnerCityAgent['city_agent_money'],4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $coupon_order['amount'] ,
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);

                $branchCityAgent = $this->agent_dao->map([
                    'id' => $partnerCityAgent['inviter_id']
                ],false); //合伙人查询分公司

                $branchAgentAmount = $branchCityAgent['city_agent_money'] - $partnerCityAgent['city_agent_money'];

                $this->agent_money_dao->create([
                    'status' => 1,
                    'agent_id' => $branchCityAgent['id'],
                    'lower_id' => $business['user_id'],
                    'business_id' => $business['id'],
                    'partner_id' => $partnerCityAgent['id'],
                    'type' => 1,
                    'order_no' => $coupon_order['order_no'],
                    'source' => 3,
                    'price_source' => 4,
                    'num' => filterMoney($coupon_order['amount'] * $branchAgentAmount,4),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'phone' => $business['tel'],
                    'amount' => $coupon_order['amount'],
                    'created_at' => time(),
                    'updated_at' => time(),
                ]);
            }

            if( $coupon_order['type'] == "1"  ) { //现金抵用券，商家不进账
                //商家分润
                $this->money_dao->create([
                    'status' => $this->money_dao::STATUS_ENABLE,
                    'user_id' => $business['user_id'],
                    'lower_id' => $coupon_order['user_id'],
                    'business_id' => $business['id'],
                    'type' => $this->money_dao::INCOME_TYPE,
                    'order_no' => $coupon_order['order_no'],
                    'source' => $this->money_dao::SOURCE_SCAN,
                    'num' => $bonus,
                    'created_at' => time(),
                    'goods_name' => $business['name'],
                    'goods_pic' => $business['logo'],
                    'amount' => $coupon_order['price'] ,
                    'service_charge' => filterMoney($coupon_order['price'] - $bonus ),
                ]);
                $this->updateMoneyBalance($business['id']);
            }

            Db::commit();
        }
        catch (CustomException $e) {
            Db::rollBack();
            $this->error($e->getMessage());
        }
        catch (\Exception $e) {
            Db::rollBack();
            $this->error('COMMON.SYSTEM_ERROR');
        }

    }

    /**
     * 用户金额更新
     * @param int $user_id
     * @return mixed
     */
    public function updateMoneyUserBalance(int $user_id)
    {
        $balance_income = $this->sumMoneyBalance( [
            'user_id' => $user_id,
            'type' => 1
        ] );

        $balance_expenditure = $this->sumMoneyBalance( [
            'user_id' => $user_id,
            'type' => 2
        ] );

        $commission = $this->sumMoneyBalance( [ // 渠道商
            'user_id' => $user_id,
            'price_source' => 3,
            'type' => 1
        ] );

        $store = $this->sumMoneyBalance( [ //商家
            'user_id' => $user_id,
            'price_source' => 2,
            'type' => 1
        ] );

        $member = $this->sumMoneyBalance( [ //会员
            'user_id' => $user_id,
            'price_source' => 1,
            'type' => 1
        ] );

        $balance = $balance_income - $balance_expenditure;

        $this->user_dao->retrieve(
            [
                'money' => $balance
            ]
            ,
            [
                'id' => $user_id
            ]
        );

        $this->redis->hSet('txh:user:'.$user_id, 'money', $balance); //总金额

        $this->redis->hSet( 'txh:user:commission:'.$user_id, 'money', $commission);

        $this->redis->hSet( 'txh:user:member:'.$user_id, 'money',$member );

        $this->redis->hSet( 'txh:user:store:'.$user_id, 'money', $store);


        return [];
    }

    /**
     * 店铺金额修改
     * @param int $business_id
     * @return mixed
     */
    public function updateMoneyBalance(int $business_id)
    {

        $business = $this->business_dao->first([
            'id' => $business_id
        ],['id','user_id']);

        $balance_income = $this->sumMoneyBalance( [
            'business_id' => $business_id,
            'type' => 1
        ] );

        $balance_expenditure = $this->sumMoneyBalance( [
            'business_id' => $business_id,
            'type' => 2
        ] );

        $balance = $balance_income - $balance_expenditure;

        $this->user_dao->retrieve(
            [
                'money' => $balance
            ]
            ,
            [
                'id' => $business['user_id']
            ]
        );

        $this->redis->hSet('txh:business:'.$business_id, 'money', $balance); //总金额


        return [];
    }

    /**
     * 获取会员实时佣金缓存
     * @param int $user_id
     * @return mixed
     */
    public function getMoneyBalanceByStore(int $user_id)
    {
        $money =  $this->redis->hGet( 'txh:user:store:'.$user_id, 'money' );

        return $money ?$money:0.00;
    }

    /**
     * 获取会员实时佣金缓存
     * @param int $user_id
     * @return mixed
     */
    public function getMoneyBalanceByMember(int $user_id)
    {
        $money =  $this->redis->hGet( 'txh:user:member:'.$user_id, 'money' );

        return $money ?$money:0.00;
    }

    /**
     * 获取会员实时佣金缓存
     * @param int $user_id
     * @return mixed
     */
    public function getMoneyBalanceByCommission(int $user_id)
    {
        $money =  $this->redis->hGet( 'txh:user:commission:'.$user_id, 'money' );

        return $money ?$money:0.00;
    }

    /**
     * 获取用户实时缓存金额 --- 总金额
     * @param int $user_id
     * @return string
     */
    public function getMoneyBalanceByUser(int $user_id)
    {
        $money =  $this->redis->hGet( 'txh:user:'.$user_id, 'money' );

        return $money ?$money:0.00;
    }

    /**
     * 获取商家实时缓存金额 --- 总金额
     * @param int $business_id
     * @return string
     */
    public function getMoneyBalance(int $business_id)
    {
        $money =  $this->redis->hGet( 'txh:business:'.$business_id, 'money' );

        return $money ?$money:0.00;
    }

    /**
     * 实时计算店铺积分 --- 按条件
     * @param array $map
     * @return mixed
     */
    public function sumMoneyBalance(array $map)
    {
        return $this->money_dao->sumMoneyBalance( $map );
    }
}